package com.itwk.gulimall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: wangke
 * @Date: 2021/01/09/下午 5:12
 * @Description:
 */
@Configuration
public class WebCorsConfiguration {
 @Bean
    public CorsWebFilter MyCorsWebFilter(){
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();// new 出CorsWebFilter的配置源
        CorsConfiguration corsConfiguration = new CorsConfiguration();  //允许跨域具体配置
        corsConfiguration.addAllowedHeader("*");//允许请求头
        corsConfiguration.addAllowedMethod("*");//请求方法
        corsConfiguration.addAllowedOrigin("*");//请求来源
        corsConfiguration.setAllowCredentials(true); //携带cookies请求

        source.registerCorsConfiguration("/**",corsConfiguration); //像配置源里添加配置信息,参数为允许路径和具体的规则 CorsConfiguration


        return new CorsWebFilter(source);
    }

}
